<?php
// Fonction de surlignage
function displayField($field){
	$display='';
	if(!empty($field)){
		$display .= highlight($field);
		$display = cut($display).', ';
	}
	return $display;
}

// Fonction pour tronquer les lignes trop longues
function cut($field){
	$max_caracteres=100;
	if (strlen($field)>$max_caracteres) {
		$field = substr($field, 0, $max_caracteres);
		$position_espace = strrpos($field, " ");
		$field = substr($field, 0, $position_espace);
		$field = $field." ...";
	}
	return $field;
}

// Fonction de surlignage
function highlight($field){
	if(isset($_GET['s'])){
		$chaine = $_GET['s'];
		$chaine_r = utf8_decode ( $chaine );
		$value2 = utf8_decode ( $field );
		$chaine_r = preg_replace ("/\\W/",".",$chaine_r);
		$value1 = preg_replace("/($chaine_r)/i", '<span class="highlight">$1</span>', $value2);
		$field = utf8_encode($value1);
	}
	return $field;
}

// Résultats et navigation du haut
if(isset($itemNumber)){
	echo '<table summary="results" style="padding-bottom:1em;"><tr><td class="roundBox">'."\n";
	echo '<span class="resultNumber">'.$itemNumber.'</span> notice(s)'."\n";
	echo 'sur <span class="resultNumber">'.$status['itemNumber'].'</span>'."\n";
	echo '</td>'."\n";
	echo '<td>&nbsp;</td>';
	echo '<td class="roundBox">'."\n";
	echo $simpleNavBar;
	echo '</td></tr></table>'."\n";
}
$itemDate = '';
echo '<div class="results">'."\n";
echo ' <div class="fieldYr"></div>'."\n";

// Pour chaque ligne
foreach($items as $key=>$value){

	if(isset($debug) and $debug==true) print_r($value);

	// Affichage notice
	$display = '<div class="notice">'."\n";

	if(!empty($value['redaction_num'])) $display .= highlight($value['redaction_num']).', ';
	// Cote
	if(!empty($value['code'])) $display .= highlight($value['code']).', ';
	// Numéro
	//	if(!empty($value['numero'])) $display .= $value['numero'].', ';

	// Titre
	if(!empty($value['url_md'])){
		$display .= '<a href="frame.php?url='.$value['url_md'].'" class="fieldArt" target="_blank" title="'.$value['title'].'" rel="superbox[iframe][920x920]">';
	}
	$display .= '<span class="fieldArt">'.highlight($value['title'].'; '.$value['title_a']).'</span>';
	if(!empty($value['url_md']))$display .= '</a> ';

	// Nouvelle fenêtre
	if(!empty($value['url_md'])){
		$display .= '<a href="'.$value['url_md'].'" class="fieldArt" target="_blank">';
		$display .= '<img src="images/open_in_new_window.gif" alt="Nouvelle fenêtre" title="Ouvrir dans une nouvelle fenêtre"/>';
		$display .= '</a>'."\n";
	}
	if(!empty($value['title']))$display .= "<br/>\n";
	// Contenu
	if(!empty($value['date'])) $display .= '<span class="fieldArt">'.$value['date'].'</span>, ';
	$display .= displayField($value['content']);
	if(!empty($value['pagination'])) $display .= $value['pagination'].' p, ' ;
	if(!empty($value['auteur'])) $display .= displayField($value['auteur']);
	if(!empty($value['see_also'])){
		$display .= 'voir aussi : ';
		$salsos=explode(';', $value['see_also']);
		foreach($salsos as $value1){
			if(!empty($value1))$display.='<a href="?a=on&amp;code='.trim($value1).'&amp;action=on">'.trim($value1).'</a>; ';

		}
	}
  // Détail AJAX
  $display .= '<a href="view.php" rel="superbox[ajax][view.php?id='.$value['id'].'&amp;';
  if(!empty($_GET['s'])) $display .= 's='.$_GET['s'];
  $display .= '][800x600]" class="fieldDesc">Détail</a> ';

	// Editions
	if(isset($_SESSION['login'])) {
	  $display .= '<a href="edit.php" style="color:red" rel="superbox[ajax][edit.php?';
	  if(!empty($_SERVER["QUERY_STRING"])){
	  	$backq = preg_split("/&id=/", $_SERVER["QUERY_STRING"]);
		  $display .= urlencode($backq[0]);
	  }
	  $display .= '&amp;id='.$value['id'].'][800x600]" class="fieldDesc">Editer</a>';
	}
	$display .= "</div>\n";;
	echo $display;
}
echo '</div>';

// Barre de navigation
if(isset($navBar)) echo '<div style="text-align:center;"><span class="roundBox">'.$navBar.'</span></div>';


?>